<?php

require_once 'Format.php';

class Validator
{

	var $rules; //i: you can directly add whole array of rules to this war or you can use function
	var $notesTemplates;
	var $lang;
	var $valid;
	
	function Validator($lang='en')
	{
		$this->lang = $lang; //i: change this from outside to get validation notes in different language
		$this->valid = false;
		
		$this->rules = array();
		
		//i: these are the default texts for validation notes
		//i: you can use them by just calling the language or you can 
		//i: manipulate or owerwrite or define-and-assign them in code in any way (add a languge...)
		$this->notesTemplates = array( 
			'en' => array(
				'is_required' => "Field %% must not be empty.",
				'is_email' => "Field %% does not look like email address.",
				'is_number' => "Field %% should have numeric value.",
				'is_oneWord' => "Field %% should contain just one word.",
				'is_pureText' => "Field %% can include just standard characters, numters and bottom-line (a-z, A-Z, 0-9,  _).",
			),
			'si' => array(
				'is_required' => "Polje %% ne sme biti prazno.",
				'is_email' => "Polje %% ni videti kot e-poštni naslov.",
				'is_number' => "Polje %% bi moralo vsebovati številsko vrednost.",
				'is_oneWord' => "Polje %% bi moralo vsebovati samo eno besedo.",
				'is_pureText' => "Polje %% lahko vsebuje le standardne mednarodne črke, števila in podčrtaj(a-z, A-Z, 0-9,  _).",
			),			
		);
	}

	function addRule($id, $rules, $name='')
	//i: you can add rules with this function or manipulate the array directly
	{
		$this->rules[] = array($id, $rules, $name);
	}

	function validate($data)
	//i: validate array of data with array of validation rules and return validation notes
	//i: $data = array('name1' => 'value1',...)
	//i: $rules = array(array('toMail', array('is_required', 'looks_email'), 'email naslovnika'), ..)
	{
		$notes = array();
		foreach($this->rules as $ruleLine)
		{
			if (isset($data[$ruleLine[0]])) 
			{
				foreach($ruleLine[1] as $rule)
				{
					if (!isset($notes[$ruleLine[0]]))
					{
						$ruleName = isset($ruleLine[2]) ? $ruleLine[2] : '';
						$note = $this->validateOne($data[$ruleLine[0]], $rule, $ruleName);
						if ($note) {$notes[$ruleLine[0]] = $note; break;}
					}
				}
			} 
			else 
			{
				trigger_error("Validation rule {$ruleLine[0]} not found in data.");
			}
		}
		if (!count($notes)) $this->valid = true;
		return $notes;
	}
	
	function validateOne($data, $rule, $name='')
	//i: validates one data with one rule as parameter (not with rules array)
	{
		if ($name) $name = "'$name'";
		switch ($rule) {
			case 'is_required':
				if (!$this->is_required($data))
					return str_replace('%%', $name, $this->notesTemplates[$this->lang]['is_required']);
				break;
			case 'is_email':
				if (!$this->is_email($data))
					return str_replace('%%', $name, $this->notesTemplates[$this->lang]['is_email']);
				break;
			case 'is_number':
				if (!$this->is_number($data))
					return str_replace('%%', $name, $this->notesTemplates[$this->lang]['is_number']);
				break;
			case 'is_oneWord':
				if (!$this->is_oneWord($data))
					return str_replace('%%', $name, $this->notesTemplates[$this->lang]['is_oneWord']);
				break;
			case 'is_pureText':
				if (!$this->is_pureText($data))
					return str_replace('%%', $name, $this->notesTemplates[$this->lang]['is_pureText']);
				break;
			default:
				trigger_error ("Rule '$rule' for validation is not supported.", E_USER_ERROR); 
		}		
		return Null;
	}
	
	function isValid()
	{
		return $this->valid;
	}
	
	//i: you can use all functions below for directly testing data... all of them can be
	//i: statically called
	
	function is_empty($text)
	//i:  Is not empty string or NULL in it.
	{
		if ( $text == '' OR is_null($text) ) return true;
		return false;
	}

	function is_required($text)
	{
		return !$this->is_empty($text);
	}
	
	function is_email( $email )
	//i:  Looks like email
	{
		if ( eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$", $email) ) return true;
		else return false;
	}

	function is_pureText( $str )
	//i:  Only chars and nums _, for usernames...
	{
		if ( eregi("^[_0-9a-z]+$", $str )) return true;
		else return false;
	}
	
	function is_number( $text )
	//i:  Looks like email
	{
		if ( is_numeric($text) ) return true;
		else return false;
	}

	function is_oneWord($text)
	//i:  Is it one word only.
	{
		if (eregi("( |\n|\t|\r)+", trim($text))) return false;
		else return true;
	}
	
	function is_short_text( $text, $max_words=20 )
	//i:  Are this few words and not too long.
	{
		return true;
		//return strpos(	
	}

	function is_normal_text( $text )
	//i:  Are there no strange longe words.
	{
		return true;
		//return strpos(	
	}

	function is_dateNull( $date )
	//i:  Are there no strange longe words.
	{
		if (is_null($date) or $date[0] == '0' or $date == '') return true;
		else return false;
	}
		
	function security_check( $text , $mode='path')
	//i:  Is not empty string or NULL in it.
	{
		switch($mode) {
			case 'path':
				return ! eregi('.*[\\|\.|\..|\/].*', $text);
			default:
				trigger_error ("mode must be 'path' ....", E_USER_ERROR);
		}
	}
}


?>